package com.mine.distinctionteam.ui.unused;


import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.ScrollView;
import android.widget.SimpleAdapter;
import android.widget.Switch;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.mine.distinctionteam.R;
import com.mine.distinctionteam.bean.unused.GoodsListBean;
import com.mine.distinctionteam.bean.unused.UnUsedBean;
import com.mine.distinctionteam.config.UnUsedUrlConfig;
import com.mine.distinctionteam.ui.good.GoodsActivity;
import com.mine.distinctionteam.ui.unused.adapter.UnUsedGridAdapter;
import com.mine.distinctionteam.widget.MyGirdView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 */
public class UnUsedFregment extends Fragment implements UnUsedContract.View {
    private Context context;
    private PopupWindow popupWindowClass;
    private PopupWindow popupWindowColor;
    private PopupWindow popupWindowMore;
    private PopupWindow popupWindowTag;
    private RadioButton button_class;
    private RadioButton button_tag;
    private RadioButton button_color;
    private RadioButton button_more;
    private PullToRefreshScrollView pullToRefreshScrollView;
    private MyGirdView myGirdView;
    private UnUsedGridAdapter adapter;
    private List<GoodsListBean> list = new ArrayList<>();
    private UnUsedPresenter presenter;
    private int cate = -1;
    private int brand = -1;
    private int condition = -1;
    private int sale = 0;
    private int source = 0;
    private int page = 1;
    private String sort;
    private boolean isClear = true;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getContext();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_unused, container, false);
        button_class = (RadioButton) view.findViewById(R.id.button_unused_class);
        button_tag = (RadioButton) view.findViewById(R.id.button_unused_tag);
        button_color = (RadioButton) view.findViewById(R.id.button_unused_color);
        button_more = (RadioButton) view.findViewById(R.id.button_unused_more);
        pullToRefreshScrollView = (PullToRefreshScrollView) view.findViewById(R.id.pulltorefreshscrollview_fragment_unused);
        myGirdView = (MyGirdView) view.findViewById(R.id.gridview_fragment_unused);
        myGirdView.setFocusable(false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        adapter = new UnUsedGridAdapter(context, list);
        myGirdView.setAdapter(adapter);
        presenter = new UnUsedPresenter(this);
        Map<String, String> map = getMap();
        presenter.getUnUsed(map);
        pullToRefreshScrollView.setMode(PullToRefreshBase.Mode.BOTH);
        initListener();
    }

    @NonNull
    private Map<String, String> getMap() {
        Map<String, String> map = new HashMap<>();
        map.put(UnUsedUrlConfig.Key.CATE, "" + cate);
        map.put(UnUsedUrlConfig.Key.BRAND, "" + brand);
        map.put(UnUsedUrlConfig.Key.CONDITION, "" + condition);
        map.put(UnUsedUrlConfig.Key.PAGE, "" + page);
        map.put(UnUsedUrlConfig.Key.SALE, "" + sale);
        map.put(UnUsedUrlConfig.Key.SOURCE, "" + source);
        if (sort != null) {
            map.put(UnUsedUrlConfig.Key.SORT, sort);
        }
        return map;
    }

    private void initListener() {
        pullToRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                page = 1;
                isClear = true;
                Map<String, String> newMap = getMap();
                presenter.getUnUsed(newMap);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {
                page++;
                isClear = false;
                Map<String, String> newMap = getMap();
                presenter.getUnUsed(newMap);
            }
        });
        myGirdView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context, GoodsActivity.class);
                intent.putExtra("id", "" + list.get(position).getId());
                startActivity(intent);
            }
        });
    }


    @Override
    public void getUnUsedSucess(UnUsedBean bean) {
        adapter.reload(bean.getGoodsList(), isClear);
        pullToRefreshScrollView.onRefreshComplete();
    }

    @Override
    public void getUnUsedFail(String erro) {
        Toast.makeText(context, erro, Toast.LENGTH_SHORT).show();
    }
}
